<!-- 编辑新增对话框js,如有需要请导出到对应的对话框js文件中 -->
var permissionSettingDialog = function () {
    var modal = $('#permissionSettingDialog');
    var _roleId;
    var _permissionIds;
    var _callback;
    var btnSave = modal.find("#btnSave");

    btnSave.click(function () {
        $(this).attr("disabled", "disabled");

        var permissionIds = modal.find("#permissionSettingJstree").jstree("get_checked");

        $.ajax({
            url: _CTX_ + "/core/role/updateRolePermissions",
            type: 'POST',
            data: $.param({"roleId": _roleId, "permissionIds": permissionIds}, true),
            cache: false,
            dataType: 'json',
            success: function (ret) {
                if (ret.state == 'ok') {
                    if (_callback && typeof(_callback) == 'function') {
                        result = _callback(ret);
                        if (typeof(result) == 'undefined' || result != false) {
                            modal.modal('hide');
                        }
                    } else {
                        modal.modal('hide');
                    }
                } else {
                    alert(ret.msg);
                }
                $(this).removeAttr("disabled");
            },
            error: function () {
                alert("保存失败，请稍后再试!");
                $(this).removeAttr("disabled");
            }
        });
    });

    function showTree() {
        modal.find("#permissionSettingJstree").jstree({
            'core': {
                'data': function (obj, callback) {
                    var params = {"parentId": obj.id, "roleId": _roleId};
                    $.ajax({
                        "url": _CTX_ + '/core/permission/list',
                        "type": "POST",
                        "data": params,
                        "async": true,
                        "cache": false,
                        "dataType": "json"
                    }).done(function (ret) {
                        if (ret.state == 'ok') {
                            var nodes = [];
                            for (var index in ret.data) {
                                nodes.push({
                                    "id": ret.data[index].uuid,
                                    "text": ret.data[index].name,
                                    "type": ret.data[index].type,
                                    "children": (ret.data[index].childrenCount > 0 ? true : false),
                                    "state": {
                                        opened: ret.data[index].childrenCount > 0,
                                        selected: $.inArray(ret.data[index].uuid, _permissionIds) > -1
                                    },
                                });
                            }
                        }
                        callback.call(this, nodes);
                    });
                },
                'check_callback': true
            },
            'checkbox': {
                'three_state': false,
                'cascade': 'up'
            },
            plugins: ["checkbox"]
        });
    }

    showTree();

    function rebindSelect(){
        var jstree = modal.find("#permissionSettingJstree").jstree(true);
        jstree.deselect_all(true);
        for(var i in _permissionIds){
            jstree.select_node(_permissionIds[i]);
        }
    }

    return {
        show: function (entity, options) {
            if (options) {
                if(options.title){
                    modal.find(".modal-title").html(options.title);
                }
                if(options.callback){
                    _callback = options.callback;
                }
            }

            if (entity && entity.roleId) {
                _roleId = entity.roleId;
                _permissionIds = entity.permissionIds?entity.permissionIds:[];
                rebindSelect();
                modal.modal('show');
            } else {
                alert("无法获取角色信息,请重试");
            }

        }
    }

}();